﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.BusinessLayer;
using FinanSoft.Entities;
using FinanSoft.Utilities;
using FinanSoft.PresentacionContabilidad;
using System.Collections;

namespace FinanSoft
{
    public partial class frmAgregarLinea : Form
    {
        frmAsientos frmAsiento;
        // Sirve para indexar los ids de las cuentas
        List<int> _accountIds;
        // Sirve para indicar cuantas cuentas son de catálogo contable y cuales son de socios de negocio
        int _catalogAccountAmount;
        // Sirve para indexar los ids de las cuentas de socio de negocios
        List<int> _auxAccountIds;
        int[] _currencyIds;

        public frmAgregarLinea(frmAsientos frmAsientoV_)
        {
            frmAsiento = frmAsientoV_;
            InitializeComponent();
            fillFields();
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            if (validateFields())
            {
                string cuenta = cmbCtaPadre.Text;
                string nombreMoneda = cmbMoneda.Text;
                int idCuenta = _accountIds[cmbCtaPadre.SelectedIndex];
                int idMoneda = _currencyIds[cmbMoneda.SelectedIndex];
                double monto = double.Parse(txtAmmount.Text);
                bool credit = rdCredit.Checked;
                int idCuentaAuxiliar = cmbCtaPadre.SelectedIndex < _catalogAccountAmount ? Constants.INVALID_ID : _auxAccountIds[cmbCtaPadre.SelectedIndex - _catalogAccountAmount];
                frmAsiento.AgregarLinea(idCuenta, cuenta, monto, idMoneda, nombreMoneda, credit, idCuentaAuxiliar);
            }
        }

        private bool validateFields()
        {

            if (txtAmmount.Text.Trim().Length == 0 || cmbCtaPadre.SelectedIndex == Constants.INVALID_ID || cmbMoneda.SelectedIndex == Constants.INVALID_ID)
            {
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void fillAccounts()
        {
            _accountIds = new List<int>();
            cmbCtaPadre.Items.Clear();
            AccountingCatalog catalog = CatalogManager.GetInstance().GetAccountingCatalog();
            foreach (Account account in catalog.PrincipalAccounts)
                addAccount(account);
            _catalogAccountAmount = cmbCtaPadre.Items.Count;
            // Comienzo a agregar las cuentas de socios de negocio
            Hashtable auxAccounts = AuxiliaryAccountManager.GetInstance().GetAuxiliarAccountsList();
            _auxAccountIds = new List<int>();
            foreach (AuxiliarAccount auxAccount in auxAccounts.Values)
            {
                // Agrego a su cuenta padre como si fuera el asiento de registro
                _accountIds.Add(auxAccount.Parent);
                // Agrego el id de la cuenta de socio de negocio
                _auxAccountIds.Add(auxAccount.AccountID);
                cmbCtaPadre.Items.Add(auxAccount.DisplayName);
            }
            cmbCtaPadre.SelectedIndex = 0;
        }

        private void addAccount(Account account)
        {
            if (!account.IsTitle)
            {
                _accountIds.Add(account.AccountID);
                cmbCtaPadre.Items.Add(account.DisplayName);
            }
            else
                foreach (Account child in account.Children.Values)
                    addAccount(child);
        }

        private void fillFields()
        {
            fillCurrencies();
            fillAccounts();
        }

        private void fillCurrencies()
        {
            int selectedId = Constants.INVALID_ID;
            List<Currency> addedCurrencies = MoneyManager.GetInstance().GetAddedCurrencies();
            _currencyIds = new int[addedCurrencies.Count + 2];
            for (int currencyIndex = 0; currencyIndex < addedCurrencies.Count; currencyIndex++)
            {
                _currencyIds[currencyIndex] = addedCurrencies[currencyIndex].Id;
                cmbMoneda.Items.Add(addedCurrencies[currencyIndex].Name);
            }
            Currency sys = MoneyManager.GetInstance().SystemCurrency;
            _currencyIds[_currencyIds.Length - 2] = sys.Id;
            cmbMoneda.Items.Add(sys.Name);
            Currency local = MoneyManager.GetInstance().LocalCurrency;
            _currencyIds[_currencyIds.Length - 1] = local.Id;
            cmbMoneda.Items.Add(local.Name);
            if (selectedId != Constants.INVALID_ID)
                cmbMoneda.SelectedIndex = selectedId;
            else
                cmbMoneda.SelectedIndex = cmbMoneda.Items.Count - 1;
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            cmbCtaPadre.SelectedIndex = Constants.INVALID_ID;
            cmbMoneda.SelectedIndex = Constants.INVALID_ID;
            txtAmmount.Text = "";
            rdCredit.Checked = true;
            rdDebit.Checked = false;

        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtAmmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }
    }
}
